﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TCPPeer
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 客户端
        /// </summary>
        Socket client = null;

        /// <summary>
        /// 创建客户端
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                System.Net.Sockets.Socket socket = new Socket(AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                //端口重用
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                int port = 60000;
                int.TryParse(portp.Text, out port);
                //socket.Bind(new IPEndPoint(IPAddress.Any, port+1));
                socket.Connect(new IPEndPoint(IPAddress.Parse(ipip.Text), port));
                Console.WriteLine("联接到远程节点：{0}，本地节点：{1}", socket.RemoteEndPoint, socket.LocalEndPoint);
                client = socket;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }
        }



        /// <summary>
        /// 创建服务端
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Click(object sender, RoutedEventArgs e)
        {
            if (client != null)
            {

                Skybot.Net.WebSockets.Sockets.AsyncSocketServer server = new Skybot.Net.WebSockets.Sockets.AsyncSocketServer((IPEndPoint)client.LocalEndPoint);
                server.Start();
                Console.WriteLine("开始监听：{0}", client.LocalEndPoint);
                server.OnAcceptSocket += Server_OnAcceptSocket;
                server.OnReceiveData += Server_OnReceiveData;

            }
        }

        private void Server_OnReceiveData(object sender, SocketAsyncEventArgs e)
        {
            Console.WriteLine("接收：{0}，数据：{1}", e.AcceptSocket.RemoteEndPoint, System.Text.Encoding.GetEncoding("GBK").GetString(e.Buffer.Take(e.BytesTransferred).ToArray()));
        }

        private void Server_OnAcceptSocket(object sender, SocketAsyncEventArgs e)
        {
            Console.WriteLine("新客户端：{0}接入", e.AcceptSocket.RemoteEndPoint);
        }
    }
}
